package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNUser;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.fragment.AlbumWallFragment;
import com.github.postsanf.yinian.utils.YNInviteSecret;
import com.umeng.analytics.MobclickAgent;

public class PicWallActivity extends BaseActivity implements View.OnClickListener {
    private ImageView iv_back;
    private ImageView iv_upLoad;
    private FrameLayout fl_contaier;
    private RelativeLayout rl_select;
    private TextView tv_import;
    private TextView tv_print;
    private TextView tv_share;
    private YNUser curUser;
    private LinearLayout ll_pic_wall_show;
    private TextView tv_pic_wall;
    private ImageView iv_pic_all;
    private ImageView iv_day;
    private ImageView iv_month;
    private boolean show = false;
    private boolean all = true;
    private boolean day = false;
    private boolean month = false;
    private RelativeLayout rl_pic_wall_show;
    private RelativeLayout rl_pic_wall;
    private RelativeLayout rl_share_select;
    private LinearLayout ll_share_link;
    private LinearLayout ll_share_qecode;
    private String firUrl = null;
    private TextView tv_transfer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_wall);
        registerBroadCast();
        curUser = application.getCurUser();
        initView();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        if (outState != null) {
            String FRAGMENTS_TAG = "android:support:fragments";
            // remove掉保存的Fragment
            outState.remove(FRAGMENTS_TAG);
        }
    }

    private void registerBroadCast() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTIONs.firstPic);
        intentFilter.addAction(ACTIONs.gone);
        registerReceiver(MyReceiver, intentFilter);
    }

    BroadcastReceiver MyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case ACTIONs.firstPic:
                    firUrl = intent.getStringExtra("firstUrl");
                    if (firUrl.equals("")) {
                        firUrl = YNSingleton.getInstance().getCurAlbum().getGpic();
                    }
                    break;
                case ACTIONs.gone:
                    rl_share_select.setVisibility(View.GONE);
                    break;
            }
        }
    };

    private void initView() {
        iv_back = ((ImageView) findViewById(R.id.iv_back));
        iv_upLoad = ((ImageView) findViewById(R.id.iv_upLoad));
        fl_contaier = ((FrameLayout) findViewById(R.id.fl_contaier));
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fl_contaier, new AlbumWallFragment()).commit();
        rl_select = ((RelativeLayout) findViewById(R.id.rl_select));
        tv_import = ((TextView) findViewById(R.id.tv_import));
        tv_print = ((TextView) findViewById(R.id.tv_print));
        tv_share = ((TextView) findViewById(R.id.tv_share));
        ll_pic_wall_show = ((LinearLayout) findViewById(R.id.ll_pic_wall_show));
        tv_pic_wall = ((TextView) findViewById(R.id.tv_pic_wall));
        rl_pic_wall = ((RelativeLayout) findViewById(R.id.rl_pic_wall));
        iv_pic_all = ((ImageView) findViewById(R.id.iv_pic_all));
        iv_day = ((ImageView) findViewById(R.id.iv_day));
        iv_month = ((ImageView) findViewById(R.id.iv_month));
        rl_pic_wall_show = ((RelativeLayout) findViewById(R.id.rl_pic_wall_show));

        rl_share_select = ((RelativeLayout) findViewById(R.id.rl_share_select));
        rl_share_select.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        ll_share_link = ((LinearLayout) findViewById(R.id.ll_share_link));
        ll_share_qecode = ((LinearLayout) findViewById(R.id.ll_share_qecode));
        tv_transfer = ((TextView) findViewById(R.id.tv_transfer));

        tv_transfer.setOnClickListener(this);
        ll_share_link.setOnClickListener(this);
        ll_share_qecode.setOnClickListener(this);

//        rl_pic_wall_show.setOnClickListener(this);
        rl_pic_wall.setOnClickListener(this);
        iv_day.setOnClickListener(this);
        iv_month.setOnClickListener(this);
        iv_pic_all.setOnClickListener(this);

        iv_back.setOnClickListener(this);
        iv_upLoad.setOnClickListener(this);
        tv_import.setOnClickListener(this);
        tv_print.setOnClickListener(this);
        tv_share.setOnClickListener(this);
        rl_select.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_upLoad:
                rl_select.setVisibility(View.VISIBLE);
                break;
            case R.id.rl_select:
                rl_select.setVisibility(View.GONE);
                break;
            //一键导入
//            case R.id.tv_import:
//                startActivity(new Intent(PicWallActivity.this, LocalManageActivity.class).putExtra("isPeopleSapce", true));
//                rl_select.setVisibility(View.GONE);
//                break;
            //冲印照片
            case R.id.tv_print:
                intent2Activity(TimeGoodsShopActivity.class);
                rl_select.setVisibility(View.GONE);
                break;
            //分享
            case R.id.tv_share:
                rl_share_select.setVisibility(View.VISIBLE);
                rl_select.setVisibility(View.GONE);
                break;
//            case R.id.rl_pic_wall:
//                show = !show;
//                if (show) {
//                    rl_pic_wall_show.setVisibility(View.VISIBLE);
//                } else {
//                    rl_pic_wall_show.setVisibility(View.GONE);
//                }
//                if (day == true) {
//                    iv_day.setImageResource(R.drawable.day_select);
//                    iv_month.setImageResource(R.drawable.month_no_select);
//                    iv_pic_all.setImageResource(R.drawable.all_pic_no_select);
//                } else if (month == true) {
//                    iv_day.setImageResource(R.drawable.day_no_select);
//                    iv_month.setImageResource(R.drawable.month_select);
//                    iv_pic_all.setImageResource(R.drawable.all_pic_no_select);
//                } else if (all == true) {
//                    iv_day.setImageResource(R.drawable.day_no_select);
//                    iv_month.setImageResource(R.drawable.month_no_select);
//                    iv_pic_all.setImageResource(R.drawable.all_pic_select);
//                }
//                break;
            case R.id.iv_day:
                day = true;
                all = false;
                month = false;
                show = !show;
                iv_day.setImageResource(R.drawable.day_select);
                iv_month.setImageResource(R.drawable.month_no_select);
                iv_pic_all.setImageResource(R.drawable.all_pic_no_select);
                sendBroadcast(new Intent(ACTIONs.day));
                rl_pic_wall_show.setVisibility(View.GONE);
                break;
            case R.id.iv_month:
                day = false;
                all = false;
                month = true;
                show = !show;
                iv_day.setImageResource(R.drawable.day_no_select);
                iv_month.setImageResource(R.drawable.month_select);
                iv_pic_all.setImageResource(R.drawable.all_pic_no_select);
                sendBroadcast(new Intent(ACTIONs.month));
                rl_pic_wall_show.setVisibility(View.GONE);
                break;
            case R.id.iv_pic_all:
                day = false;
                all = true;
                month = false;
                show = !show;
                iv_day.setImageResource(R.drawable.day_no_select);
                iv_month.setImageResource(R.drawable.month_no_select);
                iv_pic_all.setImageResource(R.drawable.all_pic_select);
                sendBroadcast(new Intent(ACTIONs.all));
                rl_pic_wall_show.setVisibility(View.GONE);
                break;
            case R.id.rl_pic_wall_show:
                show = !show;
                rl_pic_wall_show.setVisibility(View.GONE);
                break;
            case R.id.ll_share_link:
                String url = "http://wx.zhuiyinanian.com/postcard/photopage_envited?code=";
                String code = "groupid=" + application.getCurGid() + "," + "userid=" + application.getCurID();
                String codeSec = null;
                try {
                    codeSec = YNInviteSecret.encryptDES(code, getResources().getString(R.string.UNKNOW_ID));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String urlSec = url + codeSec;
                String title = curUser.getUnickname() + "和你分享了" + YNSingleton.getInstance().getCurAlbum().getGname() + "的照片墙";
                String avaUrl = curUser.getUpic();
                startActivity(new Intent(PicWallActivity.this, PicWallShareActivity.class)
                        .putExtra("uelSec", urlSec).putExtra("title", title).putExtra("avaUrl", avaUrl));
                break;
            case R.id.ll_share_qecode:
                String urlCode = "http://wx.zhuiyinanian.com/postcard/photopage_envited?code=";
                String codeQe = "groupid=" + application.getCurGid() + "," + "userid=" + application.getCurID();
                String codeSecCode = null;
                try {
                    codeSecCode = YNInviteSecret.encryptDES(codeQe, getResources().getString(R.string.UNKNOW_ID));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String urlSecCode = urlCode + codeSecCode;
                Intent intent = new Intent(PicWallActivity.this, PicWallShareActivity.class);
                intent.putExtra("QRcode", "QRcode");
                intent.putExtra("UrlShare", urlSecCode);
                intent.putExtra("firstPic", firUrl);
                intent.putExtra("nickName", application.getCurUser().getUnickname() + "在忆年小伙伴私密相册中和你分享了一面照片墙");
                intent.putExtra("QRtext", "长按识别二维码查看全部照片");
                startActivity(intent);
                break;
            case R.id.tv_transfer:
                startActivity(new Intent(PicWallActivity.this, TransferActivity.class));
                rl_select.setVisibility(View.GONE);
                finish();
                break;
        }
    }

    private long fristTime;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(MyReceiver);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
        fristTime = System.currentTimeMillis();
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
        Intent intent = new Intent(ACTIONs.record);
        intent.putExtra("key", "photoWall_3");
        intent.putExtra("value", new Long(System.currentTimeMillis() - fristTime).intValue());
        sendBroadcast(intent);
    }
}
